package com.business.frame.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.business.base.BaseController;
import com.business.base.Pager;
import com.business.frame.po.TableBean;
import com.business.frame.service.FieldService;
import com.business.frame.service.TableService;
import com.common.ErrorLog;
import com.common.NumberProcess;
import com.common.RenderPage;
import com.common.StringProcess;
import com.common.utils.SpringContextHolder;

@Controller
@RequestMapping(value = "/frame/table")
public class TableController {

	@Autowired
	private TableService tableService;
	
	@Autowired
	private FieldService fieldService;

	@RequestMapping({ "/list" })
	public String list(ModelMap model) {

		return "frame/table/list";
	}

	@RequestMapping({ "/detail" })
	public String detail(HttpServletRequest request, ModelMap model) {

		try
		{
			String tableId = StringProcess.getRequest(request.getParameter("tableId"), "");

			//初始化控件
			
			// 类型下拉
			List<Map<String, Object>> dbList = new ArrayList<Map<String, Object>>();
			Map<String, Object> dbItem = new HashMap<String, Object>();
			dbItem.put("code", "BusinessDb");
			dbItem.put("text", "BusinessDb");
			dbList.add(dbItem);
			dbItem = new HashMap<String, Object>();
			dbItem.put("code", "FormDb");
			dbItem.put("text", "FormDb");
			dbList.add(dbItem);
			dbItem = new HashMap<String, Object>();
			dbItem.put("code", "FrameDb");
			dbItem.put("text", "FrameDb");
			dbList.add(dbItem);
			dbItem = new HashMap<String, Object>();
			dbItem.put("code", "SystemDB");
			dbItem.put("text", "SystemDB(临时)");
			dbList.add(dbItem);
			
			
			String action = "";
			TableBean bean=new TableBean();
			if (tableId.length() == 0) {
				action = "insert";
				tableId = StringProcess.getUUID();
				bean.setTableId(tableId);
			} else {
				action = "update";
				bean=tableService.getBeanByKeyId(tableId);
			}

			model.put("action", action);
			model.put("dbList", dbList);
			model.put("bean", bean);
			
		}
		catch(Exception e)
		{
			ErrorLog.OnWrite(e);
		}
		
		return "frame/table/detail";
	}

	@RequestMapping("/query")
	public void query(Integer pageIndex, Integer pageSize, HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {
			
		try
		{
			Map<String, Object> search = new HashMap<String, Object>();
			Map<String, Object> searchField;
			
			//页面查询
			String tableName = request.getParameter("txtTableName");
			if(tableName!=null)
			{
				tableName = tableName.trim();
				if (tableName.length() > 0) {
					search.put("tableName", "%"+tableName+"%");
				}
			}
			//页面查询
			
			Pager<Map<String, Object>> result = tableService.selectPage(pageIndex,pageSize,search);
			
			int resultCount = result.getList().size();
			for (int i = 0; i < resultCount; i++) {
				Map<String, Object> item = result.getList().get(i);
				
				String tableId=StringProcess.convertToString(item.get("TableId"));
				
				searchField = new HashMap<String, Object>();
				searchField.put("tableId", tableId);
				searchField.put("getCount", "1");
				List<Map<String, Object>> fieldList=fieldService.select(searchField);
				int fieldCount=0;
				if (fieldList.size() > 0) {
					fieldCount = NumberProcess.convertToNumber(fieldList.get(0).get("GETCOUNTVALUE"));
				}
				item.put("fieldCount", fieldCount);
				
				item = BaseController.AddChangeRow(SpringContextHolder.getBean(TableService.class), result.getList(), item, result.getTotal(), pageIndex, i, result.getPages(), pageSize, resultCount,"TableId","OrderNumber", search);
				
			}
			
			RenderPage.JsonByObject(response, result);
			
		}
		catch(Exception e)
		{
			ErrorLog.OnWrite(e);
		}
		
	}

	@RequestMapping(params = "method=delete", method = RequestMethod.POST)
	public void del(HttpServletRequest request, HttpServletResponse response, String selectitem) {
		// TODO Auto-generated method stub

		try
		{
			String[] tableIdArray = selectitem.split(",");
	
			for (String tableId : tableIdArray) {
				if(tableId.length()>0)
				{
					tableService.delete(tableId);
				}
			}
	
			RenderPage.JsonByString(response, "1");
		}
		catch(Exception e)
		{
			ErrorLog.OnWrite(e);
		}
		
	}

	@RequestMapping(params = "method=save", method = RequestMethod.POST)
	public void save(HttpServletRequest request, HttpServletResponse response, String action) {

		try
		{
			String tableId = request.getParameter("txtTableId");
			String dbName = request.getParameter("lstDbName");
			String tableName = request.getParameter("txtTableName");
			String tableText = request.getParameter("txtTableText");
			String orderNumber = request.getParameter("txtOrderNumber");
	
			Map<String, Object> model = new HashMap<String, Object>();
			model.put("tableId", tableId);
			model.put("tableName", tableName);
			model.put("dbName", dbName);
			model.put("tableText", tableText);
			model.put("orderNumber", orderNumber);
	
			if (action.equals("insert")) {
				tableService.insert(model);
			} else if (action.equals("update")) {
				tableService.update(model);
			}
	
			RenderPage.JsonByString(response, "1");
		}
		catch(Exception e)
		{
			ErrorLog.OnWrite(e);
		}
		
	}

}
